** European Election Study
cd "/Users/mmoral/Dropbox/Sabancı University/-ONGOING/On the Relationship between Party Polarization and Citizen Polarization/EES"

*****
use "Raw/EES1989_ZA2320.dta",clear
keep if var004==3
keep zanr var002 var003 var005 var025 var026 var027 var202-var212 var284 var285
rename (zanr var002 var003 var025 var026 var027 var202 var284 var285) (study ID country elig turnout lastvote resleft age educ)
rename (var203-var212) left#, addnumber
saveold "ees89 v1.dta", replace

use "Raw/EES1994_ZA2865.dta", clear
keep if survey==411
keep zanr id country v89_* v90 v91_bel-v91_wge v114 v118_bel-v128_bel v345 v347
rename (zanr id country v114 v90 v345 v347) (study ID country resleft turnout educ age)
foreach x in lastvote{
gen `x'=.
}
foreach y in bel den ege fra gb gre irl ita lux net nir por spa wge{
replace lastvote=v91_`y' if missing(lastvote)
}
forval k=1/10{
gen left`k'=.
capture foreach y in bel den ege fra gb gre irl ita lux net nir por spa wge{
local j=117+`k'
replace left`k'=v`j'_`y' if missing(left`k')
}
}
keep study ID country resleft turnout lastvote *left* educ age
saveold "ees94 v1.dta", replace

use "Raw/EES1999_P1560a.dta", clear
gen age=1999-var160
keep study var001 var003 var096 var097 var117-var133 var161 age
rename (var001 var003 var097 var161) (ID country lastvote educ)
rename (var117-var133) left#, addnumber
saveold "ees99 v1.dta", replace

use "Raw/EES2004_ZA4566_v2.dta", clear
gen age=2004-v218 if v218!=9999
keep election country idnum v113 v114 v134-v148 v216 age
rename (election country idnum v113 v134 v216) (study country ID lastvote resleft educ)
rename (v135-v148) left#, addnumber
saveold "ees04 v1.dta", replace

use "Raw/EES2009_ZA5055_v1.1.1.dta", clear
gen age=2009-q103 if q103!=7777
keep t001 t100 t102 q27 q28 q46 q47_p1-q47_p15 q100 age
rename (t001 t100 t102 q27 q46 q100) (study ID country lastvote resleft educ) 
rename (q47_p1-q47_p15) left#, addnumber
saveold "ees09 v1.dta", replace

use "Raw/EES2014_ZA5160_v4.dta", clear
tab p13_intlang if countrycode==1056, gen(flanders)
keep za_nr flanders2 countrycode respid qpp4 qpp5_ees qpp6_ees qpp13 qpp14_1-qpp14_8 d8 vd11
rename (za_nr countrycode respid qpp4 qpp5_ees qpp13 d8 vd11) (study country ID turnout lastvote resleft educ age)
rename (qpp14_1-qpp14_8) left#, addnumber
saveold "ees14 v1.dta", replace

use "Raw/EES2019 Voter Study early release.dta", clear
tab lang_BE, gen(flanders)
keep flanders2 countrycode respid Q9 Q11 q13_* D2_1 EDU D4_1
gen age=2019-D4_1
rename (countrycode respid Q9 Q11 D2_1) (country ID lastvote resleft educ)
rename (q13_1-q13_9) left#, addnumber
saveold "ees19 v1.dta", replace

*****
use "ees89 v1.dta", clear
recode study (2320=1989)
recode country (1=1250) (2=1826) (3=1276) (4=1380) (5=1528) (6=1208) (7=1056) (8=1442) (9=1372) (10=18261) (11=1300) (12=1724) (13=1620), gen(iso)
drop if elig==2
drop elig
recode lastvote (90=.a)
recode turnout (2=0), gen(vote)
foreach var of varlist *left*{ 		/* 1-10 scale converted to 0-10 */
replace `var'=(10*`var'-10)/9
}
gen last=age-6 if educ==10
recode last (9/14=1) (15=2) (16=3) (17=4) (18=5) (19=6) (20=7) (21=8) (22/74=9) (else=.a)
gen educs=educ
replace educs=last if educs==10
drop last educ age
saveold "ees89 v2.dta", replace

use "ees94 v1.dta", clear 
recode study (2865=1994)
recode country (1=1056) (2=1208) (3=1276) (4=1300) (5=1380) (6=1724) (7=1250) (8=1372) (9=18261) (10=1442) (11=1528) (12=1620) (13=1826) (14=12761), gen(iso)
recode resleft left* (11/99=.a)
recode lastvote (95/99=.a)
recode turnout (1=1) (else=0), gen(vote)
foreach var of varlist *left*{ 		/* 1-10 scale converted to 0-10 */
replace `var'=(10*`var'-10)/9
}
replace educ=age-6 if educ==0
rename educ educs
drop age
saveold "ees94 v2.dta", replace

use "ees99 v1.dta", clear 
drop study
gen study=1999
recode country (1=1040) (2/3=1056) (4=1826) (5=1208) (6=1246) (7=1250) (8=1276) (9=1300) (10=1372) (11=1380) (12=1442) (13=1528) (14=18261) (15=1620) (16=1724) (17=1752), gen(iso) /* Belgium-Flanders and Belgium-Wallonia are merged */
recode left* (-1=.a) (97/99=.a)
rename left1 resleft
rename left# left#, renumber 
recode lastvote (97/99=.a)
gen vote=!missing(lastvote)
foreach var of varlist *left*{ 		/* 1-10 scale converted to 0-10 */
replace `var'=(10*`var'-10)/9
}
replace educ=age-6 if educ==0
recode educ (98/99=.a), gen(educs)
drop educ age
saveold "ees99 v2.dta", replace

use "ees04 v1.dta", clear 
recode country (1=1040) (2=1056) (3=1826) (4=1196) (5=1203) (6=1208) (7=1233) (8=1246) (9=1250) (10=1276) (11=1300) (12=1348) (13=1372) (14=1380) (15=1428) (16=1440) (17=1442) (19=1528) ///
(20=18261) (21=1616) (22=1620) (23=1703) (24=1705) (25=1724) (26=1752), gen(iso)
recode *left* (97/99=.a)
recode lastvote (91/95=.a) (97/99=.a)
gen vote=!missing(lastvote)
foreach var of varlist *left*{ 				/* 1-10 scale converted to 0-10 */
replace `var'=(10*`var'-10)/9 if iso!=1752 	/* 0-10 scale is used in Sweden */
}
replace educ=age-6 if educ==97
recode educ (99=.a), gen(educs)
drop educ age 
saveold "ees04 v2.dta", replace

use "ees09 v1.dta", clear
drop study
gen study=2009
gen iso=country
recode *left* (77/999=.a) 
recode lastvote (77/93=.a) (96/99=.a)
gen vote=!missing(lastvote)
replace educ=age-6 if educ==98
recode educ (77/88=.a), gen(educs)
drop educ age 
saveold "ees09 v2.dta", replace

use "ees14 v1.dta", clear
replace study=2014
gen iso=country
replace country=10561 if flanders==1 		/* Belgium-Flanders and Belgium-Wallonia are merged */
forval i=1/7{ 
gen left`=`i'+8'=left`i' if country==10561
replace left`i'=.a if country==10561
}
recode *left* (-99/-7=.a)
foreach var of varlist *left* { 			/* 1-11 scale converted to 0-10 */
replace `var'=`var'-1
}
recode turnout (2=0), gen(vote)
gen last=age-6 if educ==4
recode last (10/15=1) (16/19=2) (20/60=3) (else=.a)
recode educ (-9/-8=.a) (5=0), gen(educs)
replace educs=last if educs==4
drop last educ age
saveold "ees14 v2.dta", replace

use "ees19 v1.dta", clear
gen study=2019
gen iso=country
replace country=10561 if flanders==1 		/* Belgium-Flanders and Belgium-Wallonia are merged */
forval i=1/9{ 
gen left`=`i'+9'=left`i' if country==10561
replace left`i'=.a if country==10561
}
recode *left* (97/99=.a)
recode lastvote (96/99=0) (else=1), gen(vote)
gen last=age-6 if EDU==97
recode last (10/15=1) (16/19=2) (20/50=3) (else=.a)
recode EDU (99=.a) (97=0), gen(educs)
replace educs=last if educs==0
drop last educ age
saveold "ees19 v2.dta", replace

*****
use "ees89 v2.dta", clear
replace left10=.a if iso==1056 	/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left3=.a if iso==1250	/* for the political knowledge scale */
replace left4=.a if iso==1250	/* 23 parties in total, not matched when merged */
replace left5=.a if iso==1250
replace left7=.a if iso==1250
replace left8=.a if iso==1250
replace left5=.a if iso==1276
replace left4=.a if iso==1300
replace left5=.a if iso==1300
replace left6=.a if iso==1300
replace left7=.a if iso==1300
replace left7=.a if iso==1372
replace left8=.a if iso==1442
replace left6=.a if iso==1528
replace left8=.a if iso==1528
replace left9=.a if iso==1528
replace left3=.a if iso==1620
replace left8=.a if iso==1620
replace left9=.a if iso==1620
replace left10=.a if iso==1620
replace left8=.a if iso==1724
replace left5=.a if iso==1826
replace left6=.a if iso==1826

forval i=1/10{
by iso, sort: egen mleft`i'=mean(left`i')
recode left`i' mleft`i' (missing=.a)
gen abserr`i'=abs(left`i'-mleft`i')
}

levelsof country, loc(levels)
foreach c of loc levels {
capture forval i=1/10{
sum abserr`i' if country==`c'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & mleft`i'!=.a & country==`c'
}
}

gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
egen polknow=rowtotal(abserr*), missing
foreach c of loc levels {
egen knowsca`c'=cut(polknow) if country==`c', group(3)
replace knowsca=knowsca`c' if country==`c'
egen knowsca_alt`c'=cut(polknow) if country==`c', group(4)
replace knowsca_alt=knowsca_alt`c' if country==`c'
_pctile educs if country==`c', p(60, 99.99)
replace educsca=0 if educs<=`r(r1)' & country==`c' & !missing(educs)
replace educsca=1 if educs>`r(r1)' & educs<=`r(r2)' & country==`c' & !missing(educs)
drop knowsca`c' knowsca_alt`c'
}
tabstat educsca, stats(min max) by(country)

gen ideoknow=0
replace ideoknow=1 if left6>left3 & left6!=.a & left3!=.a & iso==1056
replace ideoknow=1 if left1<left3 & left1!=.a & left3!=.a & iso==1208
replace ideoknow=1 if left2<left9 & left2!=.a & left9!=.a & iso==1250
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1276
replace ideoknow=1 if left2>left1 & left2!=.a & left1!=.a & iso==1300
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1372
replace ideoknow=1 if left5>left1 & left5!=.a & left1!=.a & iso==1380
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1442
replace ideoknow=1 if left2>left1 & left2!=.a & left1!=.a & iso==1528
replace ideoknow=1 if left5>left4 & left5!=.a & left4!=.a & iso==1620
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1724
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1826

by iso, sort: egen rescenter=mean(resleft)

by iso, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by iso, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by iso, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by iso, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by iso, sort: egen medianv=median(resleft)
by iso, sort: egen resleftvot=sd(resleft) if vote==1
by iso, sort: egen resleftabs=sd(resleft) if vote==0
by iso, sort: egen resleftsop3=sd(resleft) if knowsca==2
by iso, sort: egen resleftsop2=sd(resleft) if knowsca==1
by iso, sort: egen resleftsop1=sd(resleft) if knowsca==0
by iso, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by iso, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by iso, sort: egen reslefteducl=sd(resleft) if educsca==0
by iso, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* study vote, by(iso)
reshape long left, i(iso) j(party)
drop if missing(left)
order iso party, first
saveold "ees89 v3.dta", replace 

use "ees94 v2.dta", clear
replace left3=.a if iso==1250 	/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left5=.a if iso==1276	/* for the political knowledge scale */
replace left5=.a if iso==1300	/* 5 parties in total, not matched when merged */
replace left5=.a if iso==1826
replace left6=.a if iso==1826

forval i=1/10{
by iso, sort: egen mleft`i'=mean(left`i')
recode left`i' mleft`i' (missing=.a)
gen abserr`i'=abs(left`i'-mleft`i')
}

levelsof country, loc(levels)
foreach c of loc levels {
capture forval i=1/10{
sum abserr`i' if country==`c'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & mleft`i'!=.a & country==`c'
}
}

gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
egen polknow=rowtotal(abserr*), missing
foreach c of loc levels {
egen knowsca`c'=cut(polknow) if country==`c', group(3)
replace knowsca=knowsca`c' if country==`c'
egen knowsca_alt`c'=cut(polknow) if country==`c', group(4)
replace knowsca_alt=knowsca_alt`c' if country==`c'
_pctile educs if country==`c', p(60, 99.99)
replace educsca=0 if educs<=`r(r1)' & country==`c' & !missing(educs)
replace educsca=1 if educs>`r(r1)' & educs<=`r(r2)' & country==`c' & !missing(educs)
drop knowsca`c' knowsca_alt`c'
}
tabstat educsca, stats(min max) by(country)

gen ideoknow=0
replace ideoknow=1 if left2>left7 & left2!=.a & left7!=.a & iso==1056
replace ideoknow=1 if left1<left7 & left1!=.a & left7!=.a & iso==1208
replace ideoknow=1 if left5>left4 & left5!=.a & left4!=.a & iso==1250
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1276
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1300
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1372
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1380
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1442
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1528
replace ideoknow=1 if left1>left4 & left1!=.a & left4!=.a & iso==1620
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1724
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1826

by iso, sort: egen rescenter=mean(resleft)

by iso, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by iso, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by iso, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by iso, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by iso, sort: egen medianv=median(resleft)
by iso, sort: egen resleftvot=sd(resleft) if vote==1
by iso, sort: egen resleftabs=sd(resleft) if vote==0
by iso, sort: egen resleftsop3=sd(resleft) if knowsca==2
by iso, sort: egen resleftsop2=sd(resleft) if knowsca==1
by iso, sort: egen resleftsop1=sd(resleft) if knowsca==0
by iso, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by iso, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by iso, sort: egen reslefteducl=sd(resleft) if educsca==0
by iso, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* study vote, by(iso)
reshape long left, i(iso) j(party)
drop if missing(left)
order iso party, first
saveold "ees94 v3.dta", replace 

use "ees99 v2.dta", clear
replace left6=.a if iso==1040	/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left5=.a if iso==1056	/* for the political knowledge scale */
replace left5=.a if iso==1208	/* 24 parties in total, not matched when merged */
replace left6=.a if iso==1208
replace left6=.a if iso==1250
replace left7=.a if iso==1250
replace left9=.a if iso==1250
replace left5=.a if iso==1276
replace left7=.a if iso==1300
replace left5=.a if iso==1380
replace left10=.a if iso==1380
replace left11=.a if iso==1380
replace left12=.a if iso==1380
replace left14=.a if iso==1380
replace left15=.a if iso==1380
replace left6=.a if iso==1442
replace left7=.a if iso==1442
replace left8=.a if iso==1528
replace left9=.a if iso==1528
replace left5=.a if iso==1620
replace left6=.a if iso==1724
replace left5=.a if iso==1826
replace left6=.a if iso==1826
replace left7=.a if iso==1826

forval i=1/16{
by country, sort: egen mleft`i'=mean(left`i')
recode left`i' mleft`i' (missing=.a)
gen abserr`i'=abs(left`i'-mleft`i')
}

levelsof country, loc(levels)
foreach c of loc levels {
capture forval i=1/16{
sum abserr`i' if country==`c'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & mleft`i'!=.a & country==`c'
}
}

gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
egen polknow=rowtotal(abserr*), missing
foreach c of loc levels {
egen knowsca`c'=cut(polknow) if country==`c', group(3)
replace knowsca=knowsca`c' if country==`c'
egen knowsca_alt`c'=cut(polknow) if country==`c', group(4)
replace knowsca_alt=knowsca_alt`c' if country==`c'
_pctile educs if country==`c', p(60, 99.99)
replace educsca=0 if educs<=`r(r1)' & country==`c' & !missing(educs)
replace educsca=1 if educs>`r(r1)' & educs<=`r(r2)' & country==`c' & !missing(educs)
drop knowsca`c' knowsca_alt`c'
}
tabstat educsca, stats(min max) by(country)

gen ideoknow=0
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1040
replace ideoknow=1 if left11<left9 & left11!=.a & left9!=.a & country==2
replace ideoknow=1 if left1<left2  & left1!=.a  & left2!=.a & country==3
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1208
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1246
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1250
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1276
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1300
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1372
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1380
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1442
replace ideoknow=1 if left1<left3 & left1!=.a & left3!=.a & iso==1528
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1620
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1724
replace ideoknow=1 if left2<left7 & left2!=.a & left7!=.a & iso==1752
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1826

by iso, sort: egen rescenter=mean(resleft)

by iso, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by iso, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by iso, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by iso, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by iso, sort: egen medianv=median(resleft)
by iso, sort: egen resleftvot=sd(resleft) if vote==1
by iso, sort: egen resleftabs=sd(resleft) if vote==0
by iso, sort: egen resleftsop3=sd(resleft) if knowsca==2
by iso, sort: egen resleftsop2=sd(resleft) if knowsca==1
by iso, sort: egen resleftsop1=sd(resleft) if knowsca==0
by iso, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by iso, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by iso, sort: egen reslefteducl=sd(resleft) if educsca==0
by iso, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* study vote, by(iso)
reshape long left, i(iso) j(party)
drop if missing(left)
order iso party, first
saveold "ees99 v3.dta", replace 

use "ees04 v2.dta", clear
replace left7=.a if iso==1208	/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left8=.a if iso==1208	/* for the political knowledge scale */
replace left1=.a if iso==1250	/* 19 parties in total, not matched when merged */
replace left2=.a if iso==1250
replace left8=.a if iso==1250
replace left10=.a if iso==1250
replace left11=.a if iso==1250
replace left6=.a if iso==1276
replace left5=.a if iso==1300
replace left5=.a if iso==1380
replace left7=.a if iso==1380
replace left11=.a if iso==1380
replace left14=.a if iso==1380
replace left1=.a if iso==1442
replace left2=.a if iso==1442
replace left8=.a if iso==1528
replace left4=.a if iso==1620
replace left8=.a if iso==1752
replace left5=.a if iso==1826

forval i=1/14{
by iso, sort: egen mleft`i'=mean(left`i')
recode left`i' mleft`i' (missing=.a)
gen abserr`i'=abs(left`i'-mleft`i')
}

levelsof country, loc(levels)
foreach c of loc levels {
capture forval i=1/14{
sum abserr`i' if country==`c'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & mleft`i'!=.a & country==`c'
}
}

gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
egen polknow=rowtotal(abserr*), missing
foreach c of loc levels {
capture noisily {
egen knowsca`c'=cut(polknow) if country==`c', group(3)
replace knowsca=knowsca`c' if country==`c'
egen knowsca_alt`c'=cut(polknow) if country==`c', group(4)
replace knowsca_alt=knowsca_alt`c' if country==`c'
_pctile educs if country==`c', p(60, 99.99)
replace educsca=0 if educs<=`r(r1)' & country==`c' & !missing(educs)
replace educsca=1 if educs>`r(r1)' & educs<=`r(r2)' & country==`c' & !missing(educs)
drop knowsca`c' knowsca_alt`c'
}
}
tabstat educsca, stats(min max) by(country)

gen ideoknow=0
replace ideoknow=1 if left2>left1 & left2!=.a & left1!=.a & iso==1040
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1196
replace ideoknow=1 if left1<left4 & left1!=.a & left4!=.a & iso==1203
replace ideoknow=1 if left6>left1 & left6!=.a & left1!=.a & iso==1208
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1233
replace ideoknow=1 if left2>left1 & left2!=.a & left1!=.a & iso==1246
replace ideoknow=1 if left7>left4 & left7!=.a & left4!=.a & iso==1250
replace ideoknow=1 if left3<left1 & left3!=.a & left1!=.a & iso==1276 /* CDU and CSU are asked in different questions */
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1300
replace ideoknow=1 if left4<left6 & left4!=.a & left6!=.a & iso==1348
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1372
replace ideoknow=1 if left9>left2 & left9!=.a & left2!=.a & iso==1380
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1428
replace ideoknow=1 if left6>left4 & left6!=.a & left4!=.a & iso==1442
replace ideoknow=1 if left2>left1 & left2!=.a & left1!=.a & iso==1528
replace ideoknow=1 if left7<left4 & left7!=.a & left4!=.a & iso==1616
replace ideoknow=1 if left6>left5 & left6!=.a & left5!=.a & iso==1620
replace ideoknow=1 if left1<left4 & left1!=.a & left4!=.a & iso==1703
replace ideoknow=1 if left3<left2 & left3!=.a & left2!=.a & iso==1705
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1724
replace ideoknow=1 if left2<left5 & left2!=.a & left5!=.a & iso==1752
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1826

by iso, sort: egen rescenter=mean(resleft)

by iso, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by iso, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by iso, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by iso, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by iso, sort: egen medianv=median(resleft)
by iso, sort: egen resleftvot=sd(resleft) if vote==1
by iso, sort: egen resleftabs=sd(resleft) if vote==0
by iso, sort: egen resleftsop3=sd(resleft) if knowsca==2
by iso, sort: egen resleftsop2=sd(resleft) if knowsca==1
by iso, sort: egen resleftsop1=sd(resleft) if knowsca==0
by iso, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by iso, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by iso, sort: egen reslefteducl=sd(resleft) if educsca==0
by iso, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* study vote, by(iso)
reshape long left, i(iso) j(party)
drop if missing(left)

* CDU and CSU are asked in different questions, only for the left-right placement. The mean score is used (6.56 and 6.97 to 6.7)
qui sum left if party==2 & iso==1276
replace left=(left+`r(mean)')/2 if party==1 & iso==1276
replace left=0 if party==2 & iso==1276
forval i=2/6{
replace left=left[_n+1] if party==`i' & iso==1276
}
drop if party==7 & iso==1276

order iso party, first
saveold "ees04 v3.dta", replace 

use "ees09 v2.dta", clear
replace left6=.a if iso==1040	/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left7=.a if iso==1040	/* for the political knowledge scale */
replace left8=.a if iso==1056	/* 23 parties in total, not matched when merged */
replace left9=.a if iso==1056
replace left8=.a if iso==1208
replace left1=.a if iso==1250
replace left8=.a if iso==1250
replace left6=.a if iso==1300
replace left6=.a if iso==1372
replace left6=.a if iso==1380
replace left7=.a if iso==1380
replace left8=.a if iso==1380
replace left7=.a if iso==1442
replace left8=.a if iso==1442
replace left11=.a if iso==1528
replace left5=.a if iso==1724
replace left13=.a if iso==1724
replace left8=.a if iso==1752
replace left4=.a if iso==1826
replace left5=.a if iso==1826
replace left6=.a if iso==1826
replace left7=.a if iso==1826
replace left8=.a if iso==1826

forval i=1/15{
by iso, sort: egen mleft`i'=mean(left`i')
recode left`i' mleft`i' (missing=.a)
gen abserr`i'=abs(left`i'-mleft`i')
}

levelsof country, loc(levels)
foreach c of loc levels {
capture forval i=1/15{
sum abserr`i' if country==`c'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & mleft`i'!=.a & country==`c'
}
}

gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
egen polknow=rowtotal(abserr*), missing
foreach c of loc levels {
egen knowsca`c'=cut(polknow) if country==`c', group(3)
replace knowsca=knowsca`c' if country==`c'
egen knowsca_alt`c'=cut(polknow) if country==`c', group(4)
replace knowsca_alt=knowsca_alt`c' if country==`c'
_pctile educs if country==`c', p(60, 99.99)
replace educsca=0 if educs<=`r(r1)' & country==`c' & !missing(educs)
replace educsca=1 if educs>`r(r1)' & educs<=`r(r2)' & country==`c' & !missing(educs)
drop knowsca`c' knowsca_alt`c'
}
tabstat educsca, stats(min max) by(country)

gen ideoknow=0
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1040
replace ideoknow=1 if left1<left4 & left1!=.a & left4!=.a & iso==1056
replace ideoknow=1 if left1<left5 & left1!=.a & left5!=.a & iso==1100
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1196
replace ideoknow=1 if left1<left4 & left1!=.a & left4!=.a & iso==1203
replace ideoknow=1 if left6>left1 & left6!=.a & left1!=.a & iso==1208
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1233
replace ideoknow=1 if left2<left3 & left2!=.a & left3!=.a & iso==1246
replace ideoknow=1 if left6>left3 & left6!=.a & left3!=.a & iso==1250
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1276
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1300
replace ideoknow=1 if left7<left1 & left7!=.a & left1!=.a & iso==1348
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1372
replace ideoknow=1 if left1>left3 & left1!=.a & left3!=.a & iso==1380
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1428
replace ideoknow=1 if left3<left1 & left3!=.a & left1!=.a & iso==1440
replace ideoknow=1 if left4>left2 & left4!=.a & left2!=.a & iso==1442
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1528
replace ideoknow=1 if left9<left10 & left9!=.a & left10!=.a & iso==1616
replace ideoknow=1 if left4<left5 & left4!=.a & left5!=.a & iso==1620
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1642
replace ideoknow=1 if left2<left3 & left2!=.a & left3!=.a & iso==1703
replace ideoknow=1 if left6<left5 & left6!=.a & left5!=.a & iso==1705
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1724
replace ideoknow=1 if left2<left5 & left2!=.a & left5!=.a & iso==1752
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1826

by iso, sort: egen rescenter=mean(resleft)

by iso, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by iso, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by iso, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by iso, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by iso, sort: egen medianv=median(resleft)
by iso, sort: egen resleftvot=sd(resleft) if vote==1
by iso, sort: egen resleftabs=sd(resleft) if vote==0
by iso, sort: egen resleftsop3=sd(resleft) if knowsca==2
by iso, sort: egen resleftsop2=sd(resleft) if knowsca==1
by iso, sort: egen resleftsop1=sd(resleft) if knowsca==0
by iso, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by iso, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by iso, sort: egen reslefteducl=sd(resleft) if educsca==0
by iso, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* study vote, by(iso)
reshape long left, i(iso) j(party)
drop if missing(left) 
order iso party, first 			/* In Spain, party codes in the codebook do not match with the data. Parties #9-13 (codebook) are recoded (in the excel file for parties) as 10, 12-15 in line w/ the data. */
saveold "ees09 v3.dta", replace 

use "ees14 v2.dta", clear
replace left6=.a if iso==1040	/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing */
replace left9=.a if iso==1056	/* for the political knowledge scale */
replace left10=.a if iso==1056	/* 18 parties in total, not matched when merged */
replace left11=.a if iso==1056	
replace left12=.a if iso==1056
replace left13=.a if iso==1056
replace left14=.a if iso==1056
replace left15=.a if iso==1056
replace left7=.a if iso==1250	
replace left8=.a if iso==1250	
replace left8=.a if iso==1300	
replace left6=.a if iso==1372	
replace left4=.a if iso==1826
replace left5=.a if iso==1826
replace left6=.a if iso==1826
replace left7=.a if iso==1826

forval i=1/15{
by country, sort: egen mleft`i'=mean(left`i')
recode left`i' mleft`i' (missing=.a)
gen abserr`i'=abs(left`i'-mleft`i')
}

levelsof country, loc(levels)
foreach c of loc levels {
capture forval i=1/15{
sum abserr`i' if country==`c'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & mleft`i'!=.a & country==`c'
}
}

gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
egen polknow=rowtotal(abserr*), missing
capture foreach c of loc levels {
egen knowsca`c'=cut(polknow) if country==`c', group(3)
replace knowsca=knowsca`c' if country==`c'
egen knowsca_alt`c'=cut(polknow) if country==`c', group(4)
replace knowsca_alt=knowsca_alt`c' if country==`c'
_pctile educs if country==`c', p(60, 99.99)
replace educsca=0 if educs<=`r(r1)' & country==`c' & !missing(educs)
replace educsca=1 if educs>`r(r1)' & educs<=`r(r2)' & country==`c' & !missing(educs)
drop knowsca`c' knowsca_alt`c'
}
tabstat educsca, stats(min max) by(country)

tabstat educsca, stats(min max mean) by(country)
replace educsca=1 if educs==3 & country==1056		/* Manual recoding for Belgium - cutpoint is .5833 */
replace educsca=1 if educs==3 & country==1208		/* Manual recoding for Denmark - cutpoint is .2711 */
replace educsca=1 if educs==3 & country==1233		/* Manual recoding for Estonia - cutpoint is .5524 */
replace educsca=1 if educs==3 & country==1246		/* Manual recoding for Finland - cutpoint is .4049 */
replace educsca=1 if educs==3 & country==1428		/* Manual recoding for Latvia - cutpoint is .5848 */
replace educsca=1 if educs==3 & country==1440		/* Manual recoding for Lithuania - cutpoint is .5731 */
replace educsca=1 if educs==3 & country==1442		/* Manual recoding for Luxembourg - cutpoint is .5985 */
replace educsca=1 if educs==3 & country==1528		/* Manual recoding for the Netherlands - cutpoint is .4702 */
replace educsca=1 if educs==3 & country==1752		/* Manual recoding for Sweden - cutpoint is .2846 */
replace educsca=1 if educs==3 & country==10561		/* Manual recoding for Belgium-Flanders - cutpoint is .5359 */

gen ideoknow=0
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1040
replace ideoknow=1 if left3<left2 & left3!=.a & left2!=.a & country==1056
replace ideoknow=1 if left10<left13 & left10!=.a & left13!=.a & country==10561
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1100
replace ideoknow=1 if left1<left3 & left1!=.a & left3!=.a & iso==1191
replace ideoknow=1 if left4<left1 & left4!=.a & left1!=.a & iso==1196
replace ideoknow=1 if left3<left7 & left3!=.a & left7!=.a & iso==1203
replace ideoknow=1 if left2>left1 & left2!=.a & left1!=.a & iso==1208
replace ideoknow=1 if left4<left3 & left4!=.a & left3!=.a & iso==1233
replace ideoknow=1 if left1>left3 & left1!=.a & left3!=.a & iso==1246
replace ideoknow=1 if left2<left1 & left2!=.a & left1!=.a & iso==1250
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1276
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1300
replace ideoknow=1 if left1<left3 & left1!=.a & left3!=.a & iso==1348
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1372
replace ideoknow=1 if left4>left1 & left4!=.a & left1!=.a & iso==1380
replace ideoknow=1 if left2<left5 & left2!=.a & left5!=.a & iso==1428
replace ideoknow=1 if left2<left4 & left2!=.a & left4!=.a & iso==1440
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1442
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1528
replace ideoknow=1 if left1<left4 & left1!=.a & left4!=.a & iso==1616
replace ideoknow=1 if left1>left3 & left1!=.a & left3!=.a & iso==1620
replace ideoknow=1 if left1<left3 & left1!=.a & left3!=.a & iso==1642
replace ideoknow=1 if left4<left1 & left4!=.a & left1!=.a & iso==1703
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1705
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1724
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1752
replace ideoknow=1 if left1>left2 & left1!=.a & left2!=.a & iso==1826

by iso, sort: egen rescenter=mean(resleft)

by iso, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by iso, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by iso, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by iso, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by iso, sort: egen medianv=median(resleft)
by iso, sort: egen resleftvot=sd(resleft) if vote==1
by iso, sort: egen resleftabs=sd(resleft) if vote==0
by iso, sort: egen resleftsop3=sd(resleft) if knowsca==2
by iso, sort: egen resleftsop2=sd(resleft) if knowsca==1
by iso, sort: egen resleftsop1=sd(resleft) if knowsca==0
by iso, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by iso, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by iso, sort: egen reslefteducl=sd(resleft) if educsca==0
by iso, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* study vote, by(iso)
reshape long left, i(iso) j(party)
drop if missing(left)
order iso party, first
saveold "ees14 v3.dta", replace 

use "ees19 v2.dta", clear
keep if inlist(iso, 1246, 1442, 1724, 1752)
replace left7=.a if iso==1442	/* Non-legislative, minor parties w/ no vote share or code (Spain 2019 to be checked when CMP2020a is available) in the CMP data are coded as missing for the political knowledge scale*/
replace left5=.a if iso==1724 	
replace left7=.a if iso==1724 	

forval i=1/9{
by country, sort: egen mleft`i'=mean(left`i')
recode left`i' mleft`i' (missing=.a)
gen abserr`i'=abs(left`i'-mleft`i')
}

levelsof country, loc(levels)
foreach c of loc levels {
capture forval i=1/8{
sum abserr`i' if country==`c'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & mleft`i'!=.a & country==`c'
}
}

gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
egen polknow=rowtotal(abserr*), missing
foreach c of loc levels {
egen knowsca`c'=cut(polknow) if country==`c', group(3)
replace knowsca=knowsca`c' if country==`c'
egen knowsca_alt`c'=cut(polknow) if country==`c', group(4)
replace knowsca_alt=knowsca_alt`c' if country==`c'
_pctile educs if country==`c', p(60, 99.99)
replace educsca=0 if educs<=`r(r1)' & country==`c' & !missing(educs)
replace educsca=1 if educs>`r(r1)' & educs<=`r(r2)' & country==`c' & !missing(educs)
drop knowsca`c' knowsca_alt`c'
}
tabstat educsca, stats(min max) by(country)

tabstat educsca, stats(min max mean) by(country)
replace educsca=0
replace educsca=1 if educs==3 & country==1246		/* Manual recoding for Finland - cutpoint is .3891 */
replace educsca=1 if educs==3 & country==1442		/* Manual recoding for Luxembourg - cutpoint is .3879 */
replace educsca=1 if educs==3 & country==1724		/* Manual recoding for Spain - cutpoint is .3409 */
replace educsca=1 if educs==3 & country==1752		/* Manual recoding for Sweden - cutpoint is .4214 */

gen ideoknow=0
replace ideoknow=1 if left1<left2 & left1!=.a & left3!=.a & iso==1246
replace ideoknow=1 if left1>left3 & left1!=.a & left2!=.a & iso==1442
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1724
replace ideoknow=1 if left1<left2 & left1!=.a & left2!=.a & iso==1752

by iso, sort: egen rescenter=mean(resleft)

by iso, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by iso, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by iso, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by iso, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0

by iso, sort: egen medianv=median(resleft)
by iso, sort: egen resleftvot=sd(resleft) if vote==1
by iso, sort: egen resleftabs=sd(resleft) if vote==0
by iso, sort: egen resleftsop3=sd(resleft) if knowsca==2
by iso, sort: egen resleftsop2=sd(resleft) if knowsca==1
by iso, sort: egen resleftsop1=sd(resleft) if knowsca==0
by iso, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by iso, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by iso, sort: egen reslefteducl=sd(resleft) if educsca==0
by iso, sort: egen reslefteduch=sd(resleft) if educsca==1

collapse (sd) resleft (mean) rescenter-reslefteduch left* study vote, by(iso)
reshape long left, i(iso) j(party)
drop if missing(left)
order iso party, first
saveold "ees19 v3.dta", replace 

*****

** Vote shares of the largest two parties in each election
/*
use "/Users/mmoral/Dropbox/Sabancı University/-ONGOING/On the Reciprocal Relationship between Party Polarization and Electoral Polarization/All Merged v1.dta", clear
encode study, gen(study_id)
keep if study_id>=5 & study_id<=10
by panel, sort: egen float voterank = rank(vote_share), field
keep if voterank==1 | voterank==2
sort study iso voterank
list iso study date pid left partyname voterank 
*/

import excel "/Users/mmoral/Dropbox/Sabancı University/-ONGOING/On the Relationship between Party Polarization and Citizen Polarization/Parties EES v10.xlsx", sheet("Stata Corrected") firstrow case(lower) clear
drop if missing(pid, iso, study)
saveold "Stata Corrected_EES.dta", replace

use "ees89 v3.dta", clear 
append using "ees94 v3.dta" "ees99 v3.dta" "ees04 v3.dta" "ees09 v3.dta" "ees14 v3.dta" "ees19 v3.dta"
keep if iso==1040 | iso==1056 | iso==1208 | iso==1246 | iso==1250 | iso==1276 | iso==1300 | iso==1372 | iso==1380 | iso==1442 | iso==1528 | iso==1620 | iso==1724 | iso==1752 | iso==1826
saveold "ees_merged v1.dta", replace
rename party pid
merge 1:1 pid iso study using "Stata Corrected_EES.dta" /* 370 unmatched: 113 non-legislative, minor parties w/ no vote share or coalitions w/ no seat share in the CMP data; 257 from unexamined party systems */
sort _merge iso study pid
gen str26 countryname=country
replace countryname="Belgium" if countryname=="Belgium (Flanders)"
gen str study2= "EES" + string(study)
drop country study _merge
rename (study2 vote) (study turnout_sample)
order study countryname iso date pid party partyname resleft resleftvot resleftabs resleftsop3 resleftsop2 resleftsop1 resleftpkn resleftpuk resleftsop_alt* rescenter medianv left, first
saveold "ees_merged v2.dta", replace
